﻿using System.Collections.Generic;
using Gp.Scripts.Core;
using UnityEngine;

namespace Gp.Scripts.Util {
    public static class PhysicsUtil {
        public static List<BaseUnit> UnitsInSphereRange(Vector3 point, float radius, int maxNum = 50) {
            Collider[] overlapSphere = new Collider[maxNum];

            Physics.OverlapSphereNonAlloc(point, radius, overlapSphere, LayerMask.GetMask("Player"));
                
            List<BaseUnit> targets = new();
            foreach (var c in overlapSphere) {
                if (c == null) {
                    continue;
                }

                if (c.TryGetComponent<BaseUnit>(out var u)) {
                    targets.Add(u);
                }
            }
            
            return targets;
        }
    }
}